package com.cts.tmall.biz.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cts.tmall.app.response.SkuResponse;
import com.cts.tmall.dal.entity.Prop;
import com.cts.tmall.dal.entity.Sku;
import com.cts.tmall.dal.entity.Spec;
import com.cts.tmall.dal.mapper.SkuMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SkuService extends ServiceImpl<SkuMapper, Sku> {

    @Autowired
    SpecService specService;

    @Autowired
    PropService propService;

    public List<SkuResponse> getSkuList(String goodsNo) {
        List<SkuResponse> skuResponseList = new ArrayList<>();
        List<Sku> skuList = this.list(new QueryWrapper<Sku>().eq("goods_no", goodsNo));
        if (skuList.size() == 0) {
            return skuResponseList;
        }
        for (Sku sku : skuList) {
            SkuResponse skuResponse = new SkuResponse();
            BeanUtils.copyProperties(sku, skuResponse);
            skuResponse.setId(sku.getId().toString());
            String propOneName = specService.getOne(new QueryWrapper<Spec>().eq("spec_no", sku.getPropOne())).getSpecName();
            skuResponse.setPropOneName(propOneName);
            if (StrUtil.isBlank(sku.getPropTwo())) {
                skuResponse.setPropTwoName("");
            } else {
                String propTwoName = specService.getOne(new QueryWrapper<Spec>().eq("spec_no", sku.getPropTwo())).getSpecName();
                skuResponse.setPropTwoName(propTwoName);
            }
            skuResponseList.add(skuResponse);
        }
        return skuResponseList;
    }

    public void reCreateSku(String goodsNo) {
        this.remove(new QueryWrapper<Sku>().eq("goods_no", goodsNo));
        List<Prop> propList = propService.list(new QueryWrapper<Prop>().eq("goods_no", goodsNo));
        if (propList.size() == 0) {
            return;
        }
        List<Sku> skuList = new ArrayList<>();
        if (propList.size() == 1) {
            List<Spec> specList = specService.list(new QueryWrapper<Spec>().eq("prop_no", propList.get(0).getPropNo()).eq("checked", 1));
            if (specList.size() == 0) {
                return;
            }
            for (Spec spec : specList) {
                Sku sku = new Sku();
                sku.setGoodsNo(goodsNo);
                sku.setPropOne(spec.getSpecNo());
                sku.setStock(0);
                sku.setPrice(0);
                sku.setSku("");
                skuList.add(sku);
            }
            this.saveBatch(skuList);
            return;
        }
        if (propList.size() == 2) {
            List<Spec> specList1 = specService.list(new QueryWrapper<Spec>().eq("prop_no", propList.get(0).getPropNo()).eq("checked", 1));
            List<Spec> specList2 = specService.list(new QueryWrapper<Spec>().eq("prop_no", propList.get(1).getPropNo()).eq("checked", 1));
            if (specList1.size() == 0 && specList2.size() == 0) {
                return;
            }
            if (specList1.size() > 0 && specList2.size() > 0) {
                for (Spec spec1 : specList1) {
                    for (Spec spec2 : specList2) {
                        Sku sku = new Sku();
                        sku.setGoodsNo(goodsNo);
                        sku.setPropOne(spec1.getSpecNo());
                        sku.setPropTwo(spec2.getSpecNo());
                        sku.setStock(0);
                        sku.setPrice(0);
                        sku.setSku("");
                        skuList.add(sku);
                    }
                }
                this.saveBatch(skuList);
                return;
            }
            List<Spec> specList;
            if (specList1.size() > 0) {
                specList = specList1;
            } else {
                specList = specList2;
            }
            for (Spec spec : specList) {
                Sku sku = new Sku();
                sku.setGoodsNo(goodsNo);
                sku.setPropOne(spec.getSpecNo());
                sku.setStock(0);
                sku.setPrice(0);
                sku.setSku("");
                skuList.add(sku);
            }
            this.saveBatch(skuList);
        }
    }
}
